package sz.szsmk.citizencard.mvp.dzsmkpresenter;

import android.util.Log;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.bean.request.GestureRequest;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.mvp.GestureMvpView;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

public class GesturePresenter extends AppBasePresenter<GestureMvpView> {
    NetworkDateSources mNetworkDateSource;

    @Inject
    public GesturePresenter() {
        getComponent().inject(this);
        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    public void createGesturePassword(String password, final String setType) {
        GestureRequest gestureRequest = new GestureRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        gestureRequest.setAccess_token(access_token);
        gestureRequest.setGesture_password(password);

        if (setType != null) {
            gestureRequest.setIs_permission("4");
        } else {
            if ("0".equals(Constant.isOpen)) {
                gestureRequest.setIs_permission("2");
            } else if ("1".equals(Constant.isOpen)) {
                gestureRequest.setIs_permission("0");
            } else {
                gestureRequest.setIs_permission("1");
            }
        }

        mNetworkDateSource.createGesturePassword(gestureRequest, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "json---" + json);
                if (setType != null) {

                    if ("0".equals(Constant.isOpen)) {
                        Constant.isOpen = "1";
                    } else if ("1".equals(Constant.isOpen)) {
                        Constant.isOpen = "2";
                    } else {
                        Constant.isOpen = "1";
                    }
                } else {
                    if ("0".equals(Constant.isOpen)) {
                        Constant.isOpen = "1";
                    } else if ("1".equals(Constant.isOpen)) {
                        Constant.isOpen = "2";
                    } else {
                        Constant.isOpen = "1";
                    }

                }

                getMvpView().success();
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                getMvpView().fail();
            }

            @Override
            protected void onNext(IbaseBean response) {
                super.onNext(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                getMvpView().fail();
            }
        });
    }

    public void validGesturePassword(String password) {
        GestureRequest gestureRequest = new GestureRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        gestureRequest.setAccess_token(access_token);
        gestureRequest.setGesture_password(password);
        gestureRequest.setIs_permission("1");
        Log.e("validGesturePassword", new Gson().toJson(gestureRequest));
        mNetworkDateSource.validGesturePassword(gestureRequest, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "json---" + json);
                getMvpView().success();
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                getMvpView().fail();
            }

            @Override
            protected void onNext(IbaseBean response) {
                super.onNext(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                getMvpView().fail();
            }
        });
    }

    public void closeGesturePassword(String password) {
        GestureRequest gestureRequest = new GestureRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        gestureRequest.setAccess_token(access_token);
        gestureRequest.setGesture_password(password);
        gestureRequest.setIs_permission("0");
        Log.e("closeGesturePassword", new Gson().toJson(gestureRequest));
        mNetworkDateSource.validGesturePassword(gestureRequest, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                String json = new Gson().toJson(response);
                L.e(Constant.TAG, "json---" + json);
                getMvpView().success();
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                getMvpView().fail();
            }

            @Override
            protected void onNext(IbaseBean response) {
                super.onNext(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                getMvpView().fail();
            }
        });
    }


    public void modifyGesturePassword(String oldpwd,String newpwd) {
        GestureRequest gestureRequest = new GestureRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        gestureRequest.setAccess_token(access_token);
        gestureRequest.setGesture_password(oldpwd);
        gestureRequest.setNew_gesture_password(newpwd);
        gestureRequest.setIs_permission("4");


        mNetworkDateSource.createGesturePassword(gestureRequest, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                String json = new Gson().toJson(response);


                Constant.isOpen = "1";
                getMvpView().success();
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                getMvpView().fail();
            }

            @Override
            protected void onNext(IbaseBean response) {
                super.onNext(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                getMvpView().fail();
            }
        });
    }

    public void resetGesturePassword(String newpwd) {
        GestureRequest gestureRequest = new GestureRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        gestureRequest.setAccess_token(access_token);
        gestureRequest.setNew_gesture_password(newpwd);
        gestureRequest.setIs_permission("3");


        mNetworkDateSource.createGesturePassword(gestureRequest, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                String json = new Gson().toJson(response);


                Constant.isOpen = "1";
                getMvpView().success();
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                getMvpView().fail();
            }

            @Override
            protected void onNext(IbaseBean response) {
                super.onNext(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                getMvpView().fail();
            }
        });
    }
}
